package system

import (
	"gitee.com/igolang/imoney/internal/app/system/routes"
	"gitee.com/igolang/imoney/internal/app/system/tools"
	"gitee.com/igolang/imoney/internal/core"
	"gitee.com/igolang/imoney/internal/core/logger"
	"gitee.com/igolang/imoney/internal/router"
	"gitee.com/igolang/pkg/boot"
	"gitee.com/igolang/pkg/xgin/xvalidator"
	"github.com/gin-gonic/gin"
)

type modules struct{}

func init() {
	core.AddService(&modules{})
}

func (m *modules) Init() error {
	logger.Log().Debug("init system modules")
	if boot.IsHttp() {
		if err := m.registerCronCmd(); err != nil {
			return err
		}
		m.validator()
		router.Add(m.router)
	}
	return nil
}

func (m *modules) Shutdown() error {
	return nil
}

func (m *modules) Name() string {
	return "system module"
}

func (m *modules) router(engine *gin.Engine) {
	routes.Init(engine)
}

func (m *modules) validator() {
	xvalidator.RegisterCustomValidation(&xvalidator.CustomValidator{
		Tag:  "mobile",
		Func: tools.Mobile,
	}, &xvalidator.CustomValidator{
		Tag:  "password",
		Func: tools.Password,
	})
}
